#ifndef _TARGETBL_INTERFACE_H_
#define _TARGETBL_INTERFACE_H_

#include "TargetBL_Config.h"

// Timer0连续计时, 每us的计数值
#define US_TO_TICK(us)  ((U32)((us) * (SYSCLK/1000000.0) / 12)) // us => tick
#define ONE_MS          ((U16)US_TO_TICK(1000))                 // 1ms
#define TEN_MS          ((U16)US_TO_TICK(10000))                // 10ms
#define FIVE_SEC        ((U16)(US_TO_TICK(5000000UL)/65536))    // 5s

// ----------------------------------
// Target BL Config Options
// ----------------------------------
#define FLASH_C2_ACCESS_LOCK_BYTE   0xFF
#ifdef BL_LOCK_FLASH
#define TGT_BL_LOCK_BYTE            (~(APP_FW_START_ADDR / TGT_FLASH_PAGE_SIZE))
#else
#define TGT_BL_LOCK_BYTE            FLASH_C2_ACCESS_LOCK_BYTE
#endif
#define TGT_BL_FW_INFOBLOCK_LENGTH  19

// Mask to clear out address bits within a page (useful for page boundary check)
// Example: For 512-byte pages, the lower 9 address bits [8:0] represent the bytes within
// a Flash page; so this mask's lower 9 bits would be set => 0x000001FF
#define TGT_FLASH_PAGE_ADDR_MASK    (TGT_FLASH_PAGE_SIZE - 1)
#define TGT_BL_PKT_SIZE_MASK        (TGT_BL_PKT_SIZE - 1)
#define TGT_BL_BUF_SIZE_MASK        (TGT_BL_BUF_SIZE - 1)

#define TGT_FLASH_PAGE_SIZE         (1 << TGT_FLASH_PAGE_SIZE_CODE)
#define APP_FW_END_ADDR             (TGT_FLASH_SIZE - TGT_FLASH_PAGE_SIZE - 1)
#define TGT_BL_PKT_SIZE             (1 << TGT_BL_PKT_SIZE_CODE)
#define TGT_BL_BUF_SIZE             (1 << TGT_BL_BUF_SIZE_CODE)

#if APP_FW_END_ADDR > 0xFFFF
#define CODE_BANK
#endif

#define APP_FW_SIG0_ADDR            (APP_FW_END_ADDR)
#define APP_FW_SIG3_ADDR            (APP_FW_END_ADDR - 3)

// ----------------------------------
// Signature Bytes
// ----------------------------------
#define SIG_BYTE0                0x3D
#define SIG_BYTE1                0xC2
#define SIG_BYTE2                0xA5
#define SIG_BYTE3                0x1B

//-----------------------------------------------------------------------------
// Enums, Structs, Unions and Typedefs - Available to all modules
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// "Public" Global Variables - Externs - Available to all modules
//-----------------------------------------------------------------------------

extern U8 Flash_Key0;
extern U8 Flash_Key1;

//-----------------------------------------------------------------------------
// "Public" Function Prototypes - Externs - Available to all modules
//-----------------------------------------------------------------------------

// This is usually defined in the STARTUP.A51 file.
void START_APPLICATION(void); 

#ifdef CODE_BANK
typedef U32 addr_t;

// This is usually defined in the L51_BANK.A51 file.
void switchbank(unsigned char bank_number);
#else// CODE_BANK
typedef U16 addr_t;
#endif// CODE_BANK

// ---------------------------------
// Device-Specific Functions
// Defined in "Fnnn_TargetBL_DevSpecific.c"
// ---------------------------------
void Device_Init(void);
U16 SystemTick();
bit Timeouts();
void ResetTimeouts();

// ---------------------------------
// Flash Read/Erase/Write Functions
// Defined in "Fnnn_Flash.c"
// ---------------------------------
void FLASH_Modify(addr_t addr, U8 byte);
U8 FLASH_Read(addr_t addr);

// -------------------------------------
// Bootloader Command Processors
// Defined in "TargetBL_Commands.c"
// -------------------------------------
void TGT_Erase_Page(U32 Addr);
void TGT_Write_Flash(U8 xdata *buf, U32 addr);

#endif // #ifndef _TARGETBL_INTERFACE_H_
